package com.jhd.jiangxi.util;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.ScanApplication;
import com.jhd.jiangxi.entity.LastInputEditText;

import java.util.List;


public class PopDeptWindow {
    private static ListView listView;
    public static void show(ScanApplication scanApplication,Context context, PopupWindow popup,boolean isCheck,TextView textView, List<String> name, List<String> id,List<String> refDeptId){
        isCheck=false;
        listView = new ListView(context);
        listView.setDivider(null);//设置分割线
        listView.setVerticalScrollBarEnabled(false);//不显示滑动条
        listView.setAdapter(new MyAdapter(scanApplication,context,popup,isCheck,name,id,refDeptId,textView));
        if(popup.isShowing())popup.dismiss();
        popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popup.setWidth(textView.getWidth());
        popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.selector_unfocused));
        popup.setContentView(listView);//设置显示内容
        popup.setOutsideTouchable(true);//点击PopupWindow以外的区域自动关闭该窗口
        popup.showAsDropDown(textView,0,5);//显示在edit控件的下面0,0代表偏移量
        textView.setBackgroundResource(R.drawable.selector_focused);
        if(name.size()>0){
            popup.setOutsideTouchable(false);//点击PopupWindow以外的区域不关闭该窗口
        }
        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                textView.setBackgroundResource(R.drawable.selector_unfocused);
            }
        });
    }
    //适配器
    private static class MyAdapter extends BaseAdapter {
        private List<String> name,id;private List<String> refDeptId;private TextView textView;private Context context;
        private ScanApplication scanApplication;private boolean isCheck;private  PopupWindow popup;
        public MyAdapter(ScanApplication scanApplication,Context context, PopupWindow popup,boolean isCheck,List<String> name,List<String>id,List<String> refDeptId,TextView textView) {
            this.name=name;
            this.id=id;
            this.textView=textView;
            this.refDeptId=refDeptId;
            this.context=context;
            this.isCheck=isCheck;
            this.popup=popup;
            this.scanApplication=scanApplication;
        }

        @Override
        public int getCount() {
            return name.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HolderView holderView=null;
            if(convertView==null){
                convertView=View.inflate(context, R.layout.item_dialog_login,null);
                holderView=new HolderView();
                holderView.text=(TextView) convertView.findViewById(R.id.tvStorage);
                convertView.setTag(holderView);
            }else{
                holderView=(HolderView) convertView.getTag();
            }

            holderView.text.setText(name.get(position));
            if(textView.getText().toString().equals(name.get(position))){
                holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
            }else{
                holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
            }

            holderView.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isCheck=true;
                    scanApplication.setSaleDeptName(name.get(position));
                    scanApplication.setSaleDeptId(id.get(position));
                    scanApplication.setRefDeptId(refDeptId.get(position));
                    EditTextDel.setCanDel((LastInputEditText) textView,false);
                    textView.setText(name.get(position));
                    textView.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                    notifyDataSetChanged();
                    textView.setBackgroundResource(R.drawable.selector_unfocused);
                    popup.dismiss();
                }
            });
            return convertView;
        }
    }


        static class HolderView{//由于这里用户都是同一个人,就省略用户图像的刷新
            private TextView text;
        }
}
